package com.allwees.data.analysis.stat.req;

import com.allwees.core.common.util.DateUtil;
import com.allwees.data.analysis.stat.EStatMode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import java.util.Date;

/**
 *
 *
 * @author yangylsky
 * @since 2020-04-14
 *
 */
@Data
@EqualsAndHashCode(callSuper=false)
@Accessors(chain = true)
public class DailyQueryStatReq extends QueryStatReq {
	@Override
	public EStatMode statMode() {
		return EStatMode.DAILY;
	}

	@Override
	public DailyQueryStatReq _clone() {
		DailyQueryStatReq req = new DailyQueryStatReq();
		BeanUtils.copyProperties(this, req);
		return req;
	}

	/**
	 * 获取上一个统计周期
	 *
	 * @return
	 */
	@Override
	public DailyQueryStatReq prevPeriod() {
		DailyQueryStatReq req = new DailyQueryStatReq();
		req.setStatType(statType)
				.setPlatform(platform)
				.setValueStatType(valueStatType)
				.setBaseStatType(baseStatType)
				.setDateFrom(getPrevDateFrom())
				.setDateTo(getPrevDateTo())
				.setQueryPrev(false)
				.setQueryDetail(false)
				.setLevel(level)
				.setBsKey(bsKey);
		return req;
	}

	@Override
	public Date offsetDateFrom(int offset) {
		return DateUtil.addDays(dateFrom, offset);
	}

	@Override
	public int interval() {
		return DateUtil.getDaysBetween(dateFrom, dateTo);
	}

	public Date getPrevDateFrom() {
		return DateUtil.addDays(dateFrom, -interval());
	}

	public Date getPrevDateTo() {
		return DateUtil.addDays(dateTo, -interval());
	}

	@Override
	protected void completion() {
		dateFrom = DateUtil.truncateToDay(dateFrom == null ? new Date() : dateFrom);
		dateTo = DateUtil.truncateToDay(dateTo == null ? new Date() : dateTo);
	}
}
